package Tk01;

public class Bank implements Runnable{
    int money;
    public void setMoney(int money){
        this.money=money;
    }

    public void run(){

        String name=Thread.currentThread().getName();
        //取钱

        if(name.equals("存钱")) {
            Overmoney(30);
        }
        //存钱
        else if(name.equals("取钱")){
            Overmoney(60);
        }
    }
    public synchronized void Overmoney(int amount){
        String name=Thread.currentThread().getName();
        if(name.equals("存钱")){
            for(int i=1;i<=3;i++){
                money=money+amount/3;
                System.out.println("存入"+amount/3+",账户上有"+money+"万元，休息一下再存");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }else{
                for(int i=1;i<=3;i++){
                    money=money-amount/3;
                    System.out.println("取出"+amount/3+",账户上有"+money+"万元，休息一下再取");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        }
    }
}
